package com.ay.weixin.autoreply.event.handle;

import java.io.IOException;
import java.sql.SQLException;

import org.lizar.config.keys.Keys;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Cell;
import org.lizar.ioc.Container;
import org.lizar.json.JObject;

import com.ay.weixin.autoreply.db.IbatisAutoReplyDao;
import com.ay.weixin.autoreply.domain.Message;

public class HandleSubscribe implements Cell {
	
	private IbatisAutoReplyDao auto_msg_dao;
	private HandleMsg handle_msg;
	
	
	
	@Override
	public void init_property() throws Exception {
		auto_msg_dao=Container.get(IbatisAutoReplyDao.class);
		handle_msg=Container.get(HandleMsg.class);
		
		
	}

	public void send_subscribe_message(long wxproject_id,JObject input,EventLoader el) throws IOException{
		Message message=null;
		try {
			message=auto_msg_dao.getEventMessage(wxproject_id,Keys.getStr("input_message_event_type.subscribe"));
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}
		
		if(message==null){
			return;
		}
		
		String msg_path=String.format(Keys.getStr("msg_template"), message.getResponse_type());
		String result=handle_msg.getMessageResult(wxproject_id,message, msg_path, el, input.getStr("to_user_name"),input.getStr("from_user_name"));
		el.text(result);
	}
	

}
